//
//  PictureLookViewController.m
//  zhaokaobao
//
//  Created by 陈一航 on 15/12/24.
//  Copyright © 2015年 oi. All rights reserved.
//

#import "PictureLookViewController.h"
#import "PictureCell.h"

@interface PictureLookViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
    int totalNum;
    float loscale;

}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UILabel *pageNums;


@end


@implementation PictureLookViewController
@synthesize page;

- (void)dealloc
{
    _collectionView.delegate=nil;
    _collectionView.dataSource=nil;

}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    

    _collectionView.minimumZoomScale=0.0;
  
    

}

- (void)viewDidAppear:(BOOL)animated

{   [super viewDidAppear:animated];
    [_collectionView registerNib:[UINib nibWithNibName:@"PictureCell" bundle:nil] forCellWithReuseIdentifier:@"cell" ];
    _pageNums.text=[NSString stringWithFormat:@"%d/%d",self.page+1,self.dataArray.count];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    
     [_collectionView setContentOffset:CGPointMake(self.page*__MainScreen_Width, 0) animated:NO];
 
   
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)goBack:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark -collectionDelegate/collectionDatasource


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count>0?self.dataArray.count:0;


}
- (CGSize)contentSizeForViewInPopover
{
     return CGSizeMake(__MainScreen_Width, __MainScreen_Height-64-44);

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(__MainScreen_Width, __MainScreen_Height-64-44);

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{
    
    PictureCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
 
       [isLogin setVendorImageUrl:cell.imgView :[isLogin isHttpHeader:self.dataArray[indexPath.row]]];
        
   
    
  
    UIPinchGestureRecognizer *pinRecognizer=[[UIPinchGestureRecognizer alloc] initWithTarget:self  action:@selector(scaGesture:)];
  
    
    [pinRecognizer setDelegate:self];
    
    [cell addGestureRecognizer:pinRecognizer];
    
    
    
    cell.imageHC.constant=__MainScreen_Height-110;
    cell.imageWC.constant=__MainScreen_Width;


    return cell;

}

-(void)scaGesture:(id)sender {
    
    
    [self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view ]];
    
    //当手指离开屏幕时,将lastscale设置为1.0
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        loscale = 1.0;
        return;
    }
    
    CGFloat scale = 1.0 - (loscale - [(UIPinchGestureRecognizer*)sender scale]);
    CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [[(UIPinchGestureRecognizer*)sender view]setTransform:newTransform];
    loscale = [(UIPinchGestureRecognizer*)sender scale];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (((scrollView.contentOffset.x+__MainScreen_Width/2)/__MainScreen_Width)>0) {
        self.page=(int)((scrollView.contentOffset.x+__MainScreen_Width/2)/__MainScreen_Width);
        _pageNums.text=[NSString stringWithFormat:@"%d/%d",self.page+1,self.dataArray.count];
    }
   
    

}

- (IBAction)saveCurrPicToPhone:(id)sender {
    
//    得到当前cell
    
    //保存当前照片到手机相册
    /**
     *  将图片保存到iPhone本地相册
     *  UIImage *image            图片对象
     *  id completionTarget       响应方法对象
     *  SEL completionSelector    方法
     *  void *contextInfo
     */
   
    //这个方法不行那就生成一个UIImage  将其存到手机中
    PictureCell *cell=( PictureCell *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:page inSection:0]];

    
    UIImageWriteToSavedPhotosAlbum(cell.imgView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void   *)contextInfo{
    
    if (error == nil) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",  nil];
        [alert show];
        
    }else{
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

@end
